<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of OrgController
 *
 * @author wangnaixin
 */
namespace Admin\Controller;

use Admin\Controller\AdminController;
use Admin\Model\OrganizationModel;
use Admin\Model\UserOrgModel;
use Admin\Model\UserModel;

/**
 * @author wangnaixin
 * @#todo 基础数据模块
 * @menu 基础数据管理
 * @menuAuto index
 * @order 60
 * @autoCheck index,getAuthList
 * @icon org.png
 */
class OrgController extends AdminController{
    //put your code here
    public $org;
    public $orgId;
    public $user;

    function __construct() {
        parent::__construct();
        $this->org = new OrganizationModel();
        $this->orgId = I('orgId');
        $this->user = new UserModel();
    }
    
    /**
     * @author hotterWang
     * @todo 部门列表
     * @menu 部门管理
     * @order 10
     */
    public function index(){
        $where = array(
            'pid' => 0,
            'status' => 1
        );
        $orgList = $this->org->where($where)->order('convert(name using gbk) asc')->select();
        $this->assign("orgList",$orgList);
        $this->display();
    }

    /**
     * @author hotterWang
     * @todo 部门删除
     */
    
    public function delOrg(){
        $where = array('id' => $this->orgId);
        $res = $this->org->where($where)->delete();
        $this->ajaxReturn($res);
    }
    
    /**
     * @todo 获取部门内容
     */
    public function getOrgInfoById(){
        $orgWhere = array(
            'pid' => 0,
            'status' => 1
        );
        if ($this->orgId) {
            $res = $this->org->where(array('id' => $this->orgId))->find();
            $orgWhere['id'] = array('neq',$res['id']);
            $this->assign("orgInfo",$res);
        }
        $this->assign('orgList',$this->org->where($orgWhere)->order('convert(name using gbk) asc')->select());
        $this->display("editOrg");
    }
    
    /**
     * @todo  保存部门内容
     */
    public function saveOrg(){
        if ($_POST['pid'] == '') {
            $_POST['pid'] = 0;
        }
        if($this->org->create()){
            if ($this->orgId) {
                $rs = $this->org->where("id=".$this->orgId)->save();
            } else {
                $rs = $this->org->add();
            }
            if($rs !== false ){
                $this->ajaxReturn($rs);
            }
        }
    }
}
